package common.project.epic.ui;

import common.project.epic.db.EpicDBAdapter;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class EpicMsgListAdapter extends SimpleCursorAdapter { 
	private Activity activity;
	private Context context; 
    private int layout; 
    
    public EpicMsgListAdapter(Activity activity, Context context, int layout, Cursor c,String[] from, int[] to) { 
        super(context, layout, c, from, to); 
        this.context = context; 
        this.layout = layout;
        this.activity = activity;
    }
	
	static class ViewHolder {
		protected TextView holdText;
		protected ImageView holdImage;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {						
		// Cursor to current item
		ViewHolder viewHolder;
        Cursor cursor = getCursor();
        if(cursor!=null) {
        	cursor.moveToPosition(position);
			if(convertView==null) {
				LayoutInflater inflater = ((Activity) context).getLayoutInflater();
				convertView = inflater.inflate(R.layout.epicmsglist_item, null, true);
				viewHolder = new ViewHolder();
				viewHolder.holdText = (TextView) convertView.findViewById(R.id.epicmsglist_item_title);
				viewHolder.holdImage = (ImageView) convertView.findViewById(R.id.epicmsglist_item_icon);
				convertView.setTag(viewHolder);
			}
			else {
	            viewHolder = (ViewHolder) convertView.getTag();
			}
			int numOfCheckedCat = cursor.getInt(cursor.getColumnIndex(EpicDBAdapter.MSG_CATEGORY_GENERAL)) +
								cursor.getInt(cursor.getColumnIndex(EpicDBAdapter.MSG_CATEGORY_GAMES)) +
								cursor.getInt(cursor.getColumnIndex(EpicDBAdapter.MSG_CATEGORY_ADVERTISEMENTS)) +
								cursor.getInt(cursor.getColumnIndex(EpicDBAdapter.MSG_CATEGORY_SUGGESTIONS)) +
								cursor.getInt(cursor.getColumnIndex(EpicDBAdapter.MSG_CATEGORY_EVENTS));
			if(numOfCheckedCat>1) {
				viewHolder.holdImage.setImageResource(R.drawable.ic_multicategories);
			}
			else {
				if(cursor.getInt(cursor.getColumnIndex(EpicDBAdapter.MSG_CATEGORY_GENERAL))==1) {
					viewHolder.holdImage.setImageResource(R.drawable.ic_generals);
				}
				if(cursor.getInt(cursor.getColumnIndex(EpicDBAdapter.MSG_CATEGORY_GAMES))==1) {
					viewHolder.holdImage.setImageResource(R.drawable.ic_leisure);
				} 
				else if(cursor.getInt(cursor.getColumnIndex(EpicDBAdapter.MSG_CATEGORY_ADVERTISEMENTS))==1) {
					viewHolder.holdImage.setImageResource(R.drawable.ic_advertisement);
				}
				else if(cursor.getInt(cursor.getColumnIndex(EpicDBAdapter.MSG_CATEGORY_SUGGESTIONS))==1) {
					viewHolder.holdImage.setImageResource(R.drawable.ic_suggestions);
				}
				else if(cursor.getInt(cursor.getColumnIndex(EpicDBAdapter.MSG_CATEGORY_EVENTS))==1) {
					viewHolder.holdImage.setImageResource(R.drawable.ic_events);
				}
			}
			if(cursor.getInt(cursor.getColumnIndex(EpicDBAdapter.MSG_READ_UNREAD))==1) {
				viewHolder.holdText.setBackgroundResource(R.color.White);
				if(cursor.getInt(cursor.getColumnIndex(EpicDBAdapter.DEFERRED_SENDING))==1) {
					viewHolder.holdText.setBackgroundResource(R.color.epic_green_2);
				}
				else {
					viewHolder.holdText.setBackgroundResource(R.color.White);
				}
			}
			else {
				viewHolder.holdText.setBackgroundResource(R.color.epic_green);
			}
			viewHolder.holdText.setText(cursor.getString(cursor.getColumnIndex(EpicDBAdapter.MSG_TITLE)));
        }
        
		return convertView;
	}
}
